package com.company.oa.hr.controller.recruitment;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.oa.common.api.Result;
import com.company.oa.hr.dto.recruitment.ResumeDTO;
import com.company.oa.hr.entity.recruitment.Resume;
import com.company.oa.hr.service.recruitment.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.Map;

@RestController
@RequestMapping("/hr/recruitment/resume")
public class ResumeController {

    @Autowired
    private ResumeService resumeService;

    /**
     * 分页查询简历列表
     */
    @GetMapping("/page")
    @PreAuthorize("hasAuthority('hr:recruitment:resume:list')")
    public Result<IPage<Map<String, Object>>> getResumePage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Resume resume) {
        IPage<Map<String, Object>> pageResult = resumeService.getResumePage(page, size, resume);
        return Result.success(pageResult);
    }

    /**
     * 获取简历详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('hr:recruitment:resume:info')")
    public Result<Resume> getResumeDetail(@PathVariable Long id) {
        Resume resume = resumeService.getById(id);
        return Result.success(resume);
    }

    /**
     * 创建简历
     */
    @PostMapping
    @PreAuthorize("hasAuthority('hr:recruitment:resume:create')")
    public Result<Boolean> createResume(@RequestBody @Valid ResumeDTO resumeDTO) {
        boolean success = resumeService.createResume(resumeDTO);
        return Result.success(success);
    }

    /**
     * 更新简历
     */
    @PutMapping
    @PreAuthorize("hasAuthority('hr:recruitment:resume:update')")
    public Result<Boolean> updateResume(@RequestBody @Valid ResumeDTO resumeDTO) {
        boolean success = resumeService.updateResume(resumeDTO);
        return Result.success(success);
    }

    /**
     * 删除简历
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('hr:recruitment:resume:delete')")
    public Result<Boolean> deleteResume(@PathVariable Long id) {
        boolean success = resumeService.deleteResume(id);
        return Result.success(success);
    }

    /**
     * 上传简历附件
     */
    @PostMapping("/upload")
    @PreAuthorize("hasAuthority('hr:recruitment:resume:upload')")
    public Result<String> uploadResumeFile(@RequestParam("file") MultipartFile file) {
        String filePath = resumeService.uploadResumeFile(file);
        return Result.success(filePath);
    }

    /**
     * 更新简历状态
     */
    @PostMapping("/status/{id}")
    @PreAuthorize("hasAuthority('hr:recruitment:resume:update')")
    public Result<Boolean> updateResumeStatus(
            @PathVariable Long id,
            @RequestParam Integer status) {
        boolean success = resumeService.updateResumeStatus(id, status);
        return Result.success(success);
    }
}